package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysExpertCustomGroup;
import com.ruoyi.system.domain.SysExpertCustomGroupExpert;
import com.ruoyi.system.service.ISysExpertCustomGroupExpertService;
import com.ruoyi.system.service.ISysExpertCustomGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义专家库Controller
 *
 * @author ruoyi
 * @date 2024-10-22
 */
@RestController
@RequestMapping("/system/expertcustomgroupexpert")
public class SysExpertCustomGroupExpertController extends BaseController {

    @Autowired
    private ISysExpertCustomGroupService sysExpertCustomGroupService;

    @Autowired
    private ISysExpertCustomGroupExpertService sysExpertCustomGroupExpertService;

    /**
     * 查询自定义专家列表
     */
    @PreAuthorize("@ss.hasPermi('system:expertcustomgroupexpert:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysExpertCustomGroupExpert sysExpertCustomGroupExpert) {
        startPage();
        List<SysExpertCustomGroupExpert> list = sysExpertCustomGroupExpertService.selectSysExpertCustomGroupExpertList(sysExpertCustomGroupExpert);
        return getDataTable(list);
    }

    /**
     * 新增自定义专家
     */
    @PreAuthorize("@ss.hasPermi('system:expertcustomgroupexpert:batchadd')")
    @Log(title = "批量添加专家", businessType = BusinessType.INSERT)
    @PostMapping("/batchadd")
    public AjaxResult batchadd(@RequestBody SysExpertCustomGroupExpert sysExpertCustomGroupExpert) {
        String[] expertIds = sysExpertCustomGroupExpert.getExpertId().split(",");
        for (String expertId : expertIds) {
            // 处理每个元素
            SysExpertCustomGroupExpert expertCustomGroupExpert = new SysExpertCustomGroupExpert();
            expertCustomGroupExpert.setCustomGroupId(sysExpertCustomGroupExpert.getCustomGroupId());
            expertCustomGroupExpert.setExpertId(expertId);
            System.out.println(expertCustomGroupExpert);
            List<SysExpertCustomGroupExpert> expertList = sysExpertCustomGroupExpertService.selectSysExpertCustomGroupExpertList(expertCustomGroupExpert);
            if (expertList.size() == 0) {
                sysExpertCustomGroupExpertService.insertSysExpertCustomGroupExpert(expertCustomGroupExpert);
            }
        }
        this.updateCustomGroupExpertNum(sysExpertCustomGroupExpert.getCustomGroupId());
        return toAjax(1);
    }

    /**
     * 导出自定义专家列表
     */
    @PreAuthorize("@ss.hasPermi('system:expertcustomgroupexpert:export')")
    @Log(title = "自定义专家库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysExpertCustomGroupExpert sysExpertCustomGroupExpert) {
        List<SysExpertCustomGroupExpert> list = sysExpertCustomGroupExpertService.selectSysExpertCustomGroupExpertList(sysExpertCustomGroupExpert);
        ExcelUtil<SysExpertCustomGroupExpert> util = new ExcelUtil<SysExpertCustomGroupExpert>(SysExpertCustomGroupExpert.class);
        util.exportExcel(response, list, "自定义专家数据");
    }

    /**
     * 删除自定义专家
     */
    @PreAuthorize("@ss.hasPermi('system:expertcustomgroupexpert:remove')")
    @Log(title = "自定义专家", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<SysExpertCustomGroupExpert> list = sysExpertCustomGroupExpertService.selectSysExpertCustomGroupExpertListByIds(ids);
        int result = sysExpertCustomGroupExpertService.deleteSysExpertCustomGroupExpertByIds(ids);
        if (list.size() > 0) {
            Map<Long, Long> map = new HashMap<>();
            for (SysExpertCustomGroupExpert row : list) {
                map.put(row.getCustomGroupId(), row.getCustomGroupId());
            }
            for (Long customGroupId : map.values()) {
                this.updateCustomGroupExpertNum(customGroupId);
            }
        }
        return toAjax(result);
    }

    /**
     * 更新数量
     */
    private void updateCustomGroupExpertNum(long customGroupId) {
        SysExpertCustomGroupExpert expertCustomGroupExpert = new SysExpertCustomGroupExpert();
        expertCustomGroupExpert.setCustomGroupId(customGroupId);
        List<SysExpertCustomGroupExpert> list = sysExpertCustomGroupExpertService.selectSysExpertCustomGroupExpertList(expertCustomGroupExpert);
        // 更新
        SysExpertCustomGroup sysExpertCustomGroup = sysExpertCustomGroupService.selectSysExpertCustomGroupById(customGroupId);
        if (sysExpertCustomGroup != null){
            sysExpertCustomGroup.setExpertNum(list.size());
            sysExpertCustomGroupService.updateSysExpertCustomGroup(sysExpertCustomGroup);
        }
    }
}
